package com.webchat.chat.web.auth;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.webchat.chat.core.baseEntity.Result;
import com.webchat.chat.core.baseEntity.ResultGenerator;
import com.webchat.chat.core.enums.UserAccountType;
import com.webchat.chat.core.tools.ChatConstant;
import com.webchat.chat.model.Role;
import com.webchat.chat.service.RolePermissionService;
import com.webchat.chat.service.RoleService;
import com.webchat.chat.web.BaseController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
* Created by admin on 2018-06-29.
*/
@RestController
@RequestMapping(ChatConstant.CONTROLLER_ROOT_PATH + "/role")
public class RoleController extends BaseController {
    @Resource
    private RoleService roleService;
    @Resource
    private RolePermissionService rolePermissionService;

    @PostMapping("/add")
    public Result add(Role role) {
        Result result = roleService.checkRole(role);
        if (result.getCode() == 400) {
            return ResultGenerator.genFailResult("角色名必填,角色类型必填");
        }
        Role roleDB= roleService.saveAndReturnRole(role);
        return ResultGenerator.genSuccessResult(roleDB);
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam String id) {
        roleService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(Role role) {
        roleService.update(role);

        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam String roleId) {
        Role role = roleService.findDetailById(roleId);
        return ResultGenerator.genSuccessResult(role);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<Role> list = roleService.findAllByTenantCode();
        Collections.sort(list,((o1, o2) ->
        {
            boolean flag = o1.getCreatedDate().before(o2.getCreatedDate());
            if (flag) {
                return 1;
            } else {
                return -1;
            }
        }
        ));
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @PostMapping("/findRolesByUserId")
    public Result findRolesByUserId(String userId) {
        List<Role> result = roleService.findRolesByUserId(userId);
        PageInfo pageInfo = new PageInfo(result);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @PostMapping("/addRoleAndPermission")
    public Result addRoleAndPermission(Role role, String[] permissionIds) {
        Result result = roleService.checkRole(role);
        if (result.getCode() == 400) {
            return ResultGenerator.genFailResult("角色名必填,角色类型必填");
        }
        rolePermissionService.addRoleAndPermission(role, permissionIds);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/updateRoleAndPermission")
    public Result updateRoleAndPermission(Role role, String[] permissionIds) {
        rolePermissionService.updateRoleAndPermission(role, permissionIds);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/findRoleType")
    public Result findRoleType() {
        UserAccountType[] values = UserAccountType.values();
        return ResultGenerator.genSuccessResult(values);
    }

    @PostMapping("/findRoleByInfo")
    public Result findRoleByInfo(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size, Role role) {
        PageHelper.startPage(page, size);
        List<Role> userList = roleService.findRoleByInfo(role);
        PageInfo pageInfo = new PageInfo(userList);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
